package com.sright.bcard.mini.web.interceptor;

import com.auth0.jwt.interfaces.Claim;
import com.sright.bcard.common.service.share.exception.base.BusinessErrorException;
import com.sright.bcard.common.service.share.exception.code.BusinessCode;
import com.sright.bcard.common.service.share.utils.WxappJWTUitls;
import com.sright.bcard.mini.web.constant.WebUrl;
import com.sright.bcard.user.share.request.TokenParam;
import io.micrometer.core.instrument.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Slf4j
public class AuthInterceptor extends HandlerInterceptorAdapter {

    List<String> excludeList = new LinkedList<>();

    {
        excludeList.add(WebUrl.Login);
        excludeList.add(WebUrl.cardInfoDetail);
        excludeList.add(WebUrl.cardMaterialDetail);
        excludeList.add(WebUrl.GetUserSetting);
        excludeList.add(WebUrl.Callback);
        excludeList.add(WebUrl.DemoCard1);
        excludeList.add(WebUrl.DemoCard2);
        excludeList.add(WebUrl.DemoCard3);
        excludeList.add(WebUrl.cardInfoMaterialDetail);
        excludeList.add(WebUrl.cardMaterialDetailNew);
        excludeList.add(WebUrl.TestLogin);
        excludeList.add(WebUrl.ShowTestButton);
        excludeList.add("test/test");
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = getAuthToken(request);
        String path = request.getServletPath();
        if (!isInPublicUrl(path)) {
            if (StringUtils.isNotBlank(token)) {
                Map<String, Claim> map = WxappJWTUitls.validateToken(token);
                if (map == null) {
                    throw new BusinessErrorException(BusinessCode.NEED_LOGIN);
                } else {
                    //获取用户信息
                    TokenParam tokenParam = new TokenParam();
                    tokenParam.setEnterpriseUserId(map.get("enterpriseUserId") == null ? 0L : map.get("enterpriseUserId").asLong());
                    tokenParam.setPersonalUserId(map.get("personalUserId") == null ? 0L : map.get("personalUserId").asLong());
                    tokenParam.setEnterpriseId(map.get("enterpriseId") == null ? 0L : map.get("enterpriseId").asLong());
                    tokenParam.setIsAuthorize(map.get("isAuthorize") == null ? 0 : map.get("isAuthorize").asInt());
                    tokenParam.setMemberId(map.get("memberId") == null ? 0L : map.get("memberId").asLong());
                    request.setAttribute("tokenParam",tokenParam);
                }
            } else {
                throw new BusinessErrorException(BusinessCode.NEED_LOGIN);
            }
        }

        return true;
    }

    boolean isInPublicUrl(String path) {
        return excludeList.contains(path);
    }

    /**
     * 获取http请求头部或者参数中的token值
     *
     * @param request http请求传递的值
     * @return 返回token
     */
    private String getAuthToken(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        if (token == null) {
            token = request.getParameter("Authorization");
        }
        return token;
    }
}

